package com.micro.user.controller;

import com.alibaba.fastjson.JSON;
import com.micro.user.api.UserAddFeign;
import com.micro.user.vo.UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * </p>
 *
 * @author zcf
 * @since 2021/4/15
 */

@RestController
public class UserAddController implements UserAddFeign {

    List<UserVo> userVos = new ArrayList<>();

    @Override
    @PostMapping("/addUser")
    public String addUser(@RequestBody UserVo user) {
        userVos.add(user);
        return "success Add user "+ JSON.toJSONString(userVos);
    }

    @Override
    @GetMapping("/listUser")
    public List<UserVo> listUser(String name, Integer age) {
        List<UserVo> userVoList = new ArrayList<>(userVos);
        if (StringUtils.isNotBlank(name)) {
            userVoList = userVoList.stream().filter(t -> t.getName().contains(name)).collect(Collectors.toList());
        }
        if (age != null) {
            userVoList = userVoList.stream().filter(t -> age.equals(t.getAge())).collect(Collectors.toList());
        }
        return userVoList;
    }

    @Override
    @PostMapping("/removeUser")
    public Integer removeUser(String name) {
        userVos = userVos.stream().filter(s -> !StringUtils.equals(name, s.getName())).collect(Collectors.toList());
        return 1;
    }

}
